package com.javayh.controller;

import com.javayh.entity.Order;
import com.javayh.util.Excel.ExcelTemplateExportUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dylan Yang
 * @Title: OrderController
 * @ProjectName lucky
 * @Description: TODO
 * @date 2018/10/510:04
 */
@Controller
@RequestMapping(value = "/excel/")
public class OrderController {

    /**
      * 导出
      * @author Dylan Yang
      */
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletResponse response){

        //模拟从数据库获取需要导出的数据
        List<Order> orderList = new ArrayList<>();
        Order order1 = new Order("1","柯南","123");
        Order order2 = new Order("2","小艾", "456");
        orderList.add(order1);
        orderList.add(order2);

        //导出操作
        ExcelTemplateExportUtil.exportExcel(orderList,"花名册","侦探",Order.class,"侦探.xls",response);
    }

    /**
      * 导入
      * @author Dylan Yang
      */
    @RequestMapping("importExcel")
    public void importExcel(){
        String filePath = "F:\\侦探.xls";
        //解析excel，
        List<Order> orderList = ExcelTemplateExportUtil.importExcel(filePath,1,1,Order.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+orderList.size()+"】行");

    }
}

